package chant.util;

import chant.model.Area;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Create-Date: 2016/6/16
 * Description:
 */
public class AreaUtil {
    static final Logger logger = Logger.getLogger(AreaUtil.class);

    private static List<Area> provinceList = new ArrayList<Area>();
    private static Map<Integer, List<Area>> cityMap = new HashMap<Integer, List<Area>>();

    public static void loadData() {
        provinceList = Area.me.find("select * from area where parent_id=0");
        for(Area province : provinceList) {
            int parent_id = province.getInt("id");
            List<Area> cityList = Area.me.find("select * from area where parent_id=?", parent_id);
            if(cityList.size() == 0) {
                // 直辖市
                cityList.add(province);
            }
            cityMap.put(province.getInt("id"), cityList);
        }
        logger.info("load province: " + provinceList.size());
        logger.info("load city block: " + cityMap.size());
    }

    public static List<Area> getProvinceList() {
        return provinceList;
    }

    public static List<Area> getCityList(int parentId) {
        return cityMap.get(parentId);
    }

}
